<template>
  <div class="order-page">
    <div class="order-page-top">
      <n-tabs v-model:value="tabValue" type="line" @updateValue="tabChange">
        <n-tab-pane name="RECHARGE" tab="充值订单"></n-tab-pane>
        <n-tab-pane name="VIP" tab="VIP订单"></n-tab-pane>
      </n-tabs>
      <div class="flex align-center flex-between">
        <div class="filter">
          <div class="form-item">
            <n-date-picker
              class="date-pick"
              v-model:value="queryForm.range"
              type="datetimerange"
              clearable
              format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss"
              @updateValue="rangeChange"
            />
          </div>
          <div class="form-item">
            <n-button type="primary" class="search-btn" @click="queryData">
              查询
            </n-button>
          </div>
        </div>
        <n-button type="primary" class="billing-btn" @click="isOpen = !isOpen">
          {{ isOpen ? '取消' : '申请开票' }}
        </n-button>
      </div>
      <div class="table">
        <n-data-table
          v-model:checked-row-keys="tableInfo.checkedRowKeys"
          :row-key="getRowKey"
          min-height="600px"
          :columns="tabValue === 'VIP' ? vipcolumns : columns"
          :data="tableInfo.tableData"
          :pagination="false"
          :bordered="false"
          @checked-row-keys-change="checkChange"
        />
      </div>
      <div v-if="isOpen" class="billing-row flex align-center flex-between">
        <div class="tip">总额达50不超过1000才可开票哦~</div>
        <div class="right">
          <div class="order-num">
            <span>已选</span>
            <span class="num">{{ checkData.length }}</span>
            <span>个订单</span>
          </div>
          <div class="total-price">
            <span>合计：</span>
            <span class="price">{{ totalPrice }}</span>
            <span>元</span>
          </div>
        </div>
      </div>
      <div class="pagination">
        <n-pagination
          v-model:page="tableInfo.queryForm.current"
          :page-count="tableInfo.total"
          :page-sizes="tableInfo.pageSizes"
          show-quick-jumper
          show-size-picker
          @updatePage="pageChange"
          @updatePageSize="sizeChange"
        ></n-pagination>
      </div>
    </div>
    <div class="order-page-bottom">
      <n-tabs default-value="开票说明" type="line">
        <n-tab-pane name="开票说明" tab="开票说明"></n-tab-pane>
      </n-tabs>
      <div class="con">
        <div class="item">
          <div class="title">1、如何开发票?</div>
          <div class="desc">
            <span>
              个人中心-我的订单页面，选择待开票订单，填写公司抬头、税号等必填信息后，点击
            </span>
            <span class="text-red">开票</span>
            <span>。</span>
          </div>
        </div>
        <div class="item">
          <div class="title">2、多少金额可以自助开票?</div>
          <div class="desc">
            <span>目前支持多笔订单合并开票，合并订单开票金额范围：</span>
            <span class="text-red">50元一1000元</span>
            <span>。单笔订单金额超过1000元开票，请联系客服处理。</span>
          </div>
        </div>
        <div class="item">
          <div class="title">3.如何开具专票?</div>
          <div class="desc">
            <span>
              目前暂不支持自助开具专票，如需要请联系客服开具。(专票金额小于500元，需要您自行承担运费，专票金额大于等于500元包邮)
            </span>
          </div>
        </div>
        <div class="item">
          <div class="title">4.如何开具个人发票?</div>
          <div class="desc">
            <span>自助开票已经支持开具个人发票，填写个人名称信息后，点击</span>
            <span class="text-red">开票</span>
            <span>。</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import {
  NButton,
  NDataTable,
  NDatePicker,
  NPagination,
  NTabPane,
  NTabs
} from 'naive-ui'
import type { DataTableColumns } from 'naive-ui'
import speciesImg from '@/assets/icon/user/species.png'
import weixinImg from '@/assets/icon/user/order/weixin.png'
import aliPayImg from '@/assets/icon/user/order/zhifubao.png'
import { ref, h, onMounted, computed, inject } from 'vue'
import { useApi } from '@/hooks/useApi'
const $clone = inject('$clone') as <T>(data: T) => T
type orderTypeEnum = 'VIP' | 'RECHARGE'
const tabValue = ref<orderTypeEnum>('RECHARGE')
const tabChange = (e: orderTypeEnum) => {
  tabValue.value = e
  checkData.value = []
  tableInfo.tableData = []
  isOpen.value = false
  queryForm.value.range = null
  tableInfo.queryForm.startTime = ''
  tableInfo.queryForm.endTime = ''
  queryData()
}
const isOpen = ref(false)
const queryForm = ref({
  range: null
})
const checkData: any = ref([])
const totalPrice = computed(() => {
  return checkData.value.reduce((acc: number, cur: any) => {
    if (tabValue.value === 'VIP') {
      return acc + cur.payMoney
    } else {
      return acc + cur.rechargeMoney
    }
  }, 0)
})
const getRowKey = (row: any) => {
  return row.id
}
const columns: DataTableColumns<any> = [
  {
    type: 'selection',
    options: ['all', 'none'],
    disabled(row) {
      return row.invoiceStatus !== 'NOT_INVOICE'
    }
  },
  {
    title: '名称/订单号',
    key: 'title',
    render(row: any) {
      return h('div', {}, [
        h(
          'div',
          {
            class: 'row-title'
          },
          [
            h('span', { class: 'title' }, `充值${row.rechargeGold}牛牛币`),
            h('img', { src: speciesImg }),
            h('span', { class: 'gold' }, '+' + row.rechargeGold)
          ]
        ),
        h(
          'div',
          {
            class: 'row-id'
          },
          row.orderNo
        )
      ])
    }
  },
  {
    title: '支付时间',
    key: 'pay_at',
    render(row: any) {
      return h(
        'div',
        {
          class: 'row-pay_at'
        },
        row.paymentTime
      )
    }
  },
  {
    title: '支付渠道',
    key: 'channel',
    render(row: any) {
      const payImg = row.payType === 'WE_CHAT' ? weixinImg : aliPayImg
      return h(
        'div',
        {
          class: 'row-pay'
        },
        [h('img', { src: payImg }), h('span', {}, '￥' + row.rechargeMoney)]
      )
    }
  },
  {
    title: '开票状态',
    key: 'status',
    render(row: any) {
      return h(
        'div',
        {
          class: `row-status ${
            row.invoiceStatus === 'NOT_INVOICE' && 'row-status-error'
          }`
        },
        row.invoiceStatus === 'NOT_INVOICE' ? '未开票' : '已开票'
      )
    }
  }
]
const vipcolumns: DataTableColumns<any> = [
  {
    type: 'selection',
    options: ['all', 'none'],
    disabled(row) {
      return row.invoiceStatus !== 'NOT_INVOICE'
    }
  },
  {
    title: '名称/订单号',
    key: 'title',
    render(row: any) {
      return h('div', {}, [
        h(
          'div',
          {
            class: 'row-title'
          },
          [
            h(
              'span',
              { class: 'title' },
              `开通${row.year + row.sendYear}年会员`
            )
          ]
        ),
        h(
          'div',
          {
            class: 'row-id'
          },
          row.orderNo
        )
      ])
    }
  },
  {
    title: '支付时间',
    key: 'pay_at',
    render(row: any) {
      return h(
        'div',
        {
          class: 'row-pay_at'
        },
        row.paymentTime
      )
    }
  },
  {
    title: '支付渠道',
    key: 'channel',
    render(row: any) {
      const payImg = row.payType === 'WE_CHAT' ? weixinImg : aliPayImg
      return h(
        'div',
        {
          class: 'row-pay'
        },
        [h('img', { src: payImg }), h('span', {}, '￥' + row.payMoney)]
      )
    }
  },
  {
    title: '开票状态',
    key: 'status',
    render(row: any) {
      return h(
        'div',
        {
          class: `row-status ${
            row.invoiceStatus === 'NOT_INVOICE' && 'row-status-error'
          }`
        },
        row.invoiceStatus === 'NOT_INVOICE' ? '未开票' : '已开票'
      )
    }
  }
]
const getApi = async () => {
  return {
    code: 0,
    data: [
      {
        total: 0,
        data: []
      }
    ]
  }
}
const { tableInfo, queryData, pageChange, sizeChange } = useApi({
  isPaging: true,
  isRequest: false,
  getData: getApi,
  initForm: {
    startTime: '',
    endTime: ''
  }
})
const rangeChange = (e: any, formatValue: any) => {
  if (formatValue) {
    tableInfo.queryForm.startTime = formatValue[0]
    tableInfo.queryForm.endTime = formatValue[1]
  } else {
    tableInfo.queryForm.startTime = ''
    tableInfo.queryForm.endTime = ''
  }
  queryData()
}
const checkChange = (e: any) => {
  const allCheckArr = $clone(checkData.value)
  const otherCheck = allCheckArr.filter((el: any) => {
    return tableInfo.tableData.every((item: any) => el.id !== item.id)
  })
  const currenCheck = $clone(tableInfo.tableData).filter((el: any) => {
    return e.includes(el.id)
  })
  checkData.value = [...otherCheck, ...currenCheck]
}

onMounted(() => {
  queryData()
})
</script>

<style scoped lang="scss">
.order-page {
  &-bottom {
    margin-top: 15px;
    background: #fff;
    border-radius: 12px;
    border: 1px solid #eeeeee;
    box-sizing: border-box;
    padding: 15px 24px;
    .con {
      .item {
        padding: 17px 0;
        border-bottom: 1px solid #eeeeee;
        &:last-child {
          border-bottom: none;
        }
        .title {
          font-size: 14px;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          color: #666666;
        }

        .desc {
          margin-top: 5px;
          font-size: 14px;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          color: #666666;
          .text-red {
            color: #f1423d;
          }
        }
      }
    }
  }
  &-top {
    background: #fff;
    border-radius: 12px;
    border: 1px solid #eeeeee;
    box-sizing: border-box;
    padding: 15px 24px;
    .filter {
      display: flex;
      flex-wrap: wrap;
      .form-item {
        margin-right: 16px;
        display: flex;
        align-items: center;
        .date-pick {
          width: 270px;
        }
        .search-btn {
          width: 82px;
          height: 36px;
          font-size: 14px;
        }
      }
    }
    .billing-btn {
      width: 132px;
      height: 36px;
      font-size: 14px;
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
    }
    .table {
      margin-top: 21px;
    }
    .billing-row {
      margin-top: 10px;
      .tip {
        font-size: 14px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        color: #999999;
      }
      .right {
        display: flex;
        align-items: center;
        .order-num {
          margin-right: 20px;
          span {
            font-size: 14px;
            font-family: PingFang SC, PingFang SC;
            font-weight: 400;
            color: #666666;
            &:first-child {
              color: #333333;
              margin-right: 4px;
            }
            &.num {
              font-size: 16px;
              font-family: PingFang SC, PingFang SC;
              font-weight: 400;
              color: #f1423d;
              margin: 0 4px;
            }
          }
        }
        .total-price {
          margin-right: 40px;
          span {
            font-size: 14px;
            font-family: PingFang SC, PingFang SC;
            font-weight: 400;
            color: #666666;
          }
          .price {
            font-size: 24px;
            font-weight: bold;
            color: #f1423d;
          }
          .billing-row-btn {
            width: 156px;
            height: 46px;
            font-size: 16px;
          }
        }
      }
    }
    .pagination {
      padding: 20px;
      display: flex;
      justify-content: center;
    }
  }
}
</style>
<style lang="scss">
.order-page {
  //数据表格
  .row-title {
    font-size: 14px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #666666;
    display: flex;
    align-items: center;
    .title {
      font-size: 14px;
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      color: #333333;
    }
    img {
      margin: 0 8px;
      width: 12px;
      height: 12px;
      display: block;
    }
    .gold {
      font-size: 14px;
      font-family: PingFang SC, PingFang SC;
      font-weight: bold;
      color: #f1423d;
    }
  }
  .row-pay {
    display: flex;
    align-items: center;
    img {
      width: 18px;
      height: 18px;
    }
    span {
      margin-left: 4px;
      font-size: 14px;
      font-family: PingFang SC, PingFang SC;
      font-weight: bold;
      color: #333333;
    }
  }

  .row-gold {
    span {
      font-size: 14px;
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      color: #333333;
      &:first-child {
        color: #f1423d;
        padding-right: 3px;
      }
    }
  }
  .row-status {
    font-size: 14px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #54c417;
    &-error {
      color: #f1423d;
    }
  }
  .row-id {
    margin-top: 10px;
    font-size: 12px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #999999;
  }
  .row-pay_at {
    font-size: 14px;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    color: #999999;
  }
}
</style>
